package icom.acm.util;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;




public class ResponseUtil {
	public static void setTextToResponse(HttpServletResponse response,
			String text) {
		PrintWriter out = null;
		try {
			response.setHeader("Content-Type", "text/html; charset=UTF-8");
            response.setHeader("Pragma", "No-cache");
            response.setDateHeader("Expires", 0);
            response.setHeader("Cache-Control", "no-cache");
			out = response.getWriter();
			out.print(text);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null)
				out.close();
		}
	}

	public static void setJsonToResponse(HttpServletResponse response,
			String JSON) {
		PrintWriter out = null;
		try {
			response.setHeader("Content-Type",
					"application/x-json; charset=UTF-8");
            response.setHeader("Pragma", "No-cache");
            response.setDateHeader("Expires", 0);
            response.setHeader("Cache-Control", "no-cache");
			out = response.getWriter();
			out.print(JSON);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null)
				out.close();
		}
	}

	public static void setXmlToResponse(HttpServletResponse response, String XML) {
		PrintWriter out = null;
		try {
			response.setHeader("Content-Type", "application/xml; charset=UTF-8");
            response.setHeader("Pragma", "No-cache");
            response.setDateHeader("Expires", 0);
            response.setHeader("Cache-Control", "no-cache");
			out = response.getWriter();
			out.print(XML);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null)
				out.close();
		}
	}

    public static void emptyListJson(HttpServletResponse response){
        setTextToResponse(response,"[]");
    }

    public static void emptyTreeJson(HttpServletResponse response){
        setTextToResponse(response,"[]");
    }

    public static void emptyListJsonWithPage(HttpServletResponse response){
        setTextToResponse(response,"{\"total\":0,\"pageSize\":0,\"rows\":[]}");

    }

    public static void emptyListJsonWithPageAndFooter(HttpServletResponse response){
        setTextToResponse(response,"{\"footer\":{},\"total\":0,\"pageSize\":0,\"rows\":[]}");
    }

    public static void main(String[] args) {
    	Map jsonmap=new HashMap();
    	//JSONObject obj=JSONObject.fromObject(jsonmap,defaultJsonConfig(true));
	}
}
